var app = getApp();
var wxCharts = require('../../utils/wxcharts.js');
var columnChart = null;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    ifShownoData: true,
    isMainChartDisplay: true,
    dataLength: '',
    // pageIndex: 1,
    // totalNumber: "",
    // size: 8,
    listCategoryOne: '',
    mychart: ''
  },
  scroll: function(e) {
    var scrollLeft = e.detail.scrollLeft;
    this.setData({ scrollLeft: scrollLeft });
  },
  getStock() {
    var that = this;
    wx.showLoading({
      title: '拼命加载中',
      mask: true
    });
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getStock', //库存统计
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userId
      },
      success: res => {
        console.log(res.data.result);
        if (res.data.code == 100) {
          wx.hideLoading();
          var arr1 = [];
          var arr2 = [];
          for (var i = 0; i < res.data.result.length; i++) {
            arr1.push(res.data.result[i].categoryName);
            arr2.push(Math.round(res.data.result[i].currentStock));
          }
          let simulationData = {
            categories: arr1,
            data: arr2
          };
          that.setData({
            ifShownoData: true,
            listCategoryOne: res.data.result,
            dataLength: arr1.length
          });
          that.getStocktwo(simulationData);
        } else {
          that.setData({ ifShownoData: false });
        }
      }
    });
  },
  getStocktwo(simulationData) {
    let res = wx.getSystemInfoSync();
    let windowWidth = res.windowWidth;
    console.log(simulationData);

    if (windowWidth < simulationData.categories.length * 70) {
      windowWidth = simulationData.categories.length * 70;
    }
    columnChart = new wxCharts({
      canvasId: 'lineCanvas',
      type: 'column',
      categories: simulationData.categories,
      legend: false,
      animation: true,
      series: [
        {
          name: '成交量',
          data: simulationData.data,
          color: '#73C1FF', // 配色，不传入则使用系统默认配色方案
          format: function(val, name) {
            return val.toFixed(0);
          }
        }
      ],
      yAxis: {
        format: function(val) {
          return val;
        },
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
      dataLabel: true,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(e) {
    this.getStock();
  }
});
